<template>
  <van-button
    :icon="value===1 ? 'good-job' : 'good-job-o'"
    :class="{
      liked: value===1,
    }"
    :loading="loading"
    @click="onLike"
  >
  </van-button>
</template>

<script>
import {addLike,deleteLike} from '@/api/atricle'
export default {
  data() {
    return {
      loading: false,
    };
  },
  props: {
    value: {
      type: [Number],
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  methods: {
    async onLike() {
      try {
          let status=-1
        if (this.value===1) {
          //  如果值是1则取消点赞
          await deleteLike(this.articleId)

        } else {
            // 点赞
            await addLike(this.articleId)
            status=1
        }
        this.$emit('input',status)
        this.$toast.success(status===1?'点赞成功':'取消点赞')
      } catch (err) {
          console.log(err)
      }
    },
  },
};
</script>

<style lang='less'scoped>
.liked {
  .van-icon {
    color: red;
  }
}
</style>